import {
  UserList,
  removeUser,
  addUser,
  editUser,
  deptQuery,
  enterpriseList,
  userDetail,
  permissionRole,
  adminEnterprise,
  roleListSelect,
  status
} from '../api/usermanage';
import { message } from 'antd/lib/index';
import checkStatus from '@/utils/status';

export default {
  namespace: 'userModels',

  state: {
    data: {
      data: [],
      pagination: {},
    },
  },
  effects: {
    //请求用户列表
    * fetch({ payload }, { call, put }) {
      const response = yield call(UserList, payload);
      yield checkStatus(response);
      yield put({
        type: 'get',
        payload: response.msg,
      });
    },
    //增加
    * add({ payload, callback }, { call, put }) {
      const response = yield call(addUser, payload);
      yield checkStatus(response);

      yield put({
        type: 'save',
        payload: response,
      });
      if (callback) callback();
    },

    * remove({ payload, callback }, { call, put }) {
      const response = yield call(removeUser, payload);
      yield checkStatus(response);

      yield put({
        type: 'userRemove',
        payload: response,
      });
      if (callback) callback(response);
    },

    * update({ payload, callback }, { call, put }) {
      const response = yield call(editUser, payload);
      yield checkStatus(response);

      yield put({
        type: 'edit',
        payload: response,
      });
      if (callback) callback();
    },

    * status({ payload, callback }, { call, put }) {
      const response = yield call(status, payload);
      yield checkStatus(response);
      if (callback) callback(response);
    },
    * getUserDesc({ payload, callback }, { call, put }) {
      const response = yield call(userDetail, payload);
      yield checkStatus(response);

      yield put({
        type: 'getdesc',
        payload: response.msg,
      });
      if (callback) callback();
    },
    * getDept({ payload, callback }, { call, put }) {
      const response = yield call(deptQuery, payload);
      yield checkStatus(response);

      yield put({
        type: 'deptList',
        payload: response.msg.data,
      });
      if (callback) callback();
    },

    * getRoleList({ payload, callback }, { call, put }) {
      const response = yield call(roleListSelect, payload);
      yield checkStatus(response);

      yield put({
        type: 'RoleList',
        payload: response.msg,
      });
      if (callback) callback(response);
    },
    * bindRole({ payload, callback }, { call, put }) {
      const response = yield call(permissionRole, payload);
      yield checkStatus(response);
      if (callback) callback(response);
    },
    * bindEnterprise({ payload, callback }, { call, put }) {
      const response = yield call(adminEnterprise, payload);
      yield checkStatus(response);
      if (callback) callback(response);
    },
    * enterpriseList({ payload, callback }, { call, put }) {
      const response = yield call(enterpriseList, payload);
      yield checkStatus(response);
      yield put({
        type: 'companyList',
        payload: response.msg,
      });
      if (callback) callback(response);
    },
  },

  reducers: {
    get(state, action) {
      console.log('action.payload', action.payload);
      state.pagination = {
        pageSize: action.payload.pageSize,
        current: action.payload.pageNo,
        total: action.payload.total,
      };
      return {
        ...state,
        data: action.payload,
      };
    },
    save(state, action) {
      if (action.payload.code === 200) {
        message.success('添加成功');
        return {
          ...state,
          data: action.payload.msg,
        };
      } else {
        message.error(action.payload.errMsg);
        return {
          ...state,
          data: [],
        };
      }

    },
    userRemove(state, action) {
      return {
        ...state,
        data: action.payload,
      };
    },
    edit(state, action) {
      if (action.payload.code === 200) {
        message.success('更新成功');
      } else {
        message.error(action.payload.errMsg);
      }
      return {
        ...state,
        data: action.payload,
      };
    },
    deptList(state, action) {
      return {
        ...state,
        deptList: action.payload,
      };
    },
    getdesc(state, action) {
      return {
        ...state,
        userDesc: action.payload,
      };
    },
    companyList(state, action) {
      return {
        ...state,
        companyList: action.payload,
      };
    },
    RoleList(state, action) {
      console.log(11111, action.payload);
      return {
        ...state,
        RoleLists: action.payload,
      };
    },
  },
};
